package team8.fitdroid;

import java.util.LinkedList;

import team8.database.databaseconnector;
import team8.helpers.numberformat;
import team8.helpers.timehelper;
import team8.helpers.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;

public class runsetup extends Activity {

	private Button startbutton;
	private Button backbutton;
	private Button resetbutton;
	private EditText timeedit;
	private EditText speededit;
	private EditText calorieedit;
	private EditText distanceedit;
	
	private boolean reset=false;
	
	private double runnerweight =70;
	private databaseconnector database;
	private user runner;
	private timehelper timeh = new timehelper();
	
	
	private Boolean[] edittest=new Boolean[]{false,false,false,false};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.runsetup);
        
        startbutton= (Button)findViewById(R.id.startbutton);
        startbutton.setOnClickListener(startbuttonListener);
        
        backbutton= (Button)findViewById(R.id.backbutton);
        backbutton.setOnClickListener(backbuttonListener);
        
        resetbutton= (Button)findViewById(R.id.resetbutton);
        resetbutton.setOnClickListener(resetbuttonListener);
        
        timeedit= (EditText)findViewById(R.id.timeedit);
        speededit= (EditText)findViewById(R.id.speededit);
        calorieedit= (EditText)findViewById(R.id.calorieedit);
        distanceedit= (EditText)findViewById(R.id.distanceedit);
        
        
        database= new databaseconnector(this);
        runner= new user(database);
		LinkedList<String> data = runner.getuserdata();
		if (data.size()>0)
		{
			if (Integer.valueOf(data.get(2))!=0)
				runnerweight = Double.valueOf(data.get(2));			
		}
		
		distanceedit.setOnFocusChangeListener(distancefocus);
		calorieedit.setOnFocusChangeListener(caloriefocus);
		timeedit.setOnFocusChangeListener(timefocus);
		speededit.setOnFocusChangeListener(speedfocus);
		
		
        
    }
    
    private OnClickListener startbuttonListener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
        	Intent i = new Intent(runsetup.this, runmanager.class);
        	runmanager.init(distanceedit.getText().toString(), calorieedit.getText().toString(), speededit.getText().toString(), timeedit.getText().toString());
            startActivity(i);
        }
    };
    
    private OnClickListener backbuttonListener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
        	Intent i = new Intent(runsetup.this, main.class);
            startActivity(i);
        	
        }
    };
    
    private OnClickListener resetbuttonListener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
        		reset=true;
    			timeedit.setEnabled(true);
    			//timeedit.setFocusable(true);
    			timeedit.setText("00:00:00");

    			distanceedit.setEnabled(true);
    			//distanceedit.setFocusable(true);
    			distanceedit.setText("0.00");

    			speededit.setEnabled(true);
    			//speededit.setFocusable(true);
    			speededit.setText("0.00");

    			calorieedit.setEnabled(true);
    			//calorieedit.setFocusable(true);
    			calorieedit.setText("0");
    			
    			edittest=new Boolean[]{false,false,false,false};
    			
    			reset=false;
    		
        }
    };
    

    
    private OnFocusChangeListener caloriefocus =new OnFocusChangeListener() {

    	@Override
    	public void onFocusChange(View v, boolean hasFocus) {
    		if (!hasFocus&&(!calorieedit.getText().toString().equalsIgnoreCase("0")) )
    		{
        		checkeditboxes(3, null);
    			double calories = Double.valueOf(calorieedit.getText().toString());
    			double distance = calories/runnerweight;
    			
        		distanceedit.setText(String.valueOf(distance));
        		Log.d("calorie",edittest[0]+" "+edittest[1]+" "+edittest[2]+" "+edittest[3]);
        		//reset=false;
    		}
    	}    	 
    	};
    
        private OnFocusChangeListener distancefocus =new OnFocusChangeListener() {

        	@Override
        	public void onFocusChange(View v, boolean hasFocus) {
        		if (!hasFocus&& (!distanceedit.getText().toString().equalsIgnoreCase("0")))
        		{
            		checkeditboxes(1, null);
        			double distance = Double.valueOf(distanceedit.getText().toString());
        			double calories = distance*runnerweight;
    	        	calorieedit.setText(String.valueOf(calories));
            		Log.d("distance",edittest[0]+" "+edittest[1]+" "+edittest[2]+" "+edittest[3]);
            		//reset=false;
        		}
        	}    	 
        	};
        private OnFocusChangeListener timefocus =new OnFocusChangeListener() {

            	@Override
            	public void onFocusChange(View v, boolean hasFocus) {
            		if (!hasFocus&&(!timeedit.getText().toString().equalsIgnoreCase("00:00:00")))
            		{
            			checkeditboxes(0, null);
                		Log.d("time",edittest[0]+" "+edittest[1]+" "+edittest[2]+" "+edittest[3]);
                		//reset=false;
            		}
            	}    	 
            	};
         private OnFocusChangeListener speedfocus =new OnFocusChangeListener() {

                	@Override
                	public void onFocusChange(View v, boolean hasFocus) {
                		if (!hasFocus&&(!speededit.getText().toString().equalsIgnoreCase("0.00")))
                		{
                			checkeditboxes(2, null);
                    		Log.d("Speed ",edittest[0]+" "+edittest[1]+" "+edittest[2]+" "+edittest[3]);
                    		//reset=false;
                		}
                	}    	 
                	};
    
    private void checkeditboxes (int editbox, Editable s)
    {	
    	
		if (!reset)
		{    	

	    	//if (counter<2)
	    		edittest[editbox]=true;
		    	int counter=0;
		    	for (int i=0;i<4;i++)
		    	{
		    		if (edittest[i])
		    			counter++;
		    	}
	    	if (counter==2)
	    	{
	
	    		if (!edittest[0])
	    		{
	    			timeedit.setEnabled(false);
	    			//timeedit.setFocusable(false);
	    			//timeedit.setText("00:50:00");
	    		}
	    		if (!edittest[1])
	    		{
	    			distanceedit.setEnabled(false);
	    			//distanceedit.setFocusable(false);
	    			//distanceedit.setText("10.00");
	    		}
	    		if (!edittest[2])
	    		{
	    			speededit.setEnabled(false);
	    			//speededit.setFocusable(false);
	    			//speededit.setText("10.00");
	    		}
	    		if (!edittest[3])
	    		{
	    			calorieedit.setEnabled(false);
	    			//calorieedit.setFocusable(false);
	    			//calorieedit.setText("250");
	    		}
	    		Log.d("FILLEDITBOX","ECH sin erem dran");
	    		filleditbox ();
	    	}
	    if (counter==1)
	    {
	    	if (edittest[1])
	    		{
    			calorieedit.setEnabled(false);
	    			
	    		}
	    	if (edittest[3])
	    		{
	    		distanceedit.setEnabled(false);

	    		}
	    }
	    	
		}
    }
    
    private void filleditbox ()
    {
    	//edittest : time,distance,speed,calorie
	    	timehelper helper = new timehelper();
	    	
	    	// time distance
	    	if (edittest[0]&&edittest[1])
	    	{
	    		double time =  helper.converttimeseconds(timeedit.getText().toString());
	    		double distance = Double.valueOf(distanceedit.getText().toString());
	    		
	    		double speed = numberformat.round2commas(distance/(time/3600));
	    		double calories = distance*runnerweight;
	    		Log.d("FILLEDITBOX", speed + " "+time+ " "+distance );
	    		speededit.setText(String.valueOf(speed));
	    		calorieedit.setText(String.valueOf(calories));
	    		
	    	}
	    	// time speed
	    	if (edittest[0]&&edittest[2])
	    	{
	    		double time =  helper.converttimeseconds(timeedit.getText().toString());
	    		double speed = Double.valueOf(speededit.getText().toString());
	    		
	    		double distance = numberformat.round2commas(speed*(time/3600));
	    		double calories = distance*runnerweight;
	    		
	    		distanceedit.setText(String.valueOf(distance));
	    		calorieedit.setText(String.valueOf(calories));
	    	}
	    	// time calorie
	    	if (edittest[0]&&edittest[3])
	    	{
	    		double time =  helper.converttimeseconds(timeedit.getText().toString());
	    		double calories = Double.valueOf(calorieedit.getText().toString());
	    		
	    		double distance = numberformat.round2commas(calories/runnerweight);
	    		double speed = numberformat.round2commas(distance/(time/3600));
	    		
	    		distanceedit.setText(String.valueOf(distance));
	    		speededit.setText(String.valueOf(speed));
	    	}
	    	//distance speed
	    	if (edittest[1]&&edittest[2])
	    	{
	    		double distance =  Double.valueOf(distanceedit.getText().toString());
	    		double speed = Double.valueOf(speededit.getText().toString());
	    		
	    		int time = (int)((distance/speed)*3600);
	    		double calorie = distance*runnerweight;
	    		
	    		timeedit.setText(timeh.secondstotime(time));
	    		calorieedit.setText(String.valueOf(calorie));
	    	}
	    	//calorie speed
	    	if (edittest[3]&&edittest[2])
	    	{
	    		double speed =  Double.valueOf(speededit.getText().toString());
	    		double calorie = Double.valueOf(calorieedit.getText().toString());
	    		
	    		double distance = calorie/runnerweight;
	    		int time = (int)((distance/speed)*3600);
	    		
	    		
	    		timeedit.setText(timeh.secondstotime(time));
	    		distanceedit.setText(String.valueOf(distance));
	    	}
	    	
    	
    }
    
    @Override
    public void onDestroy()
	{
		database.close();
		super.onDestroy();
	}
}
